/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewOrderJPanel.java
 *
 * Created on Jun 8, 2011, 12:39:52 PM
 */
package presentation.guiForVirtualTrading;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JTextField;

import presentation.ComboKeyHandler;
import presentation.mainJFrame;
import business.virtualTrading.Order;
import dataAccess.databaseManagement.entity.AssetEntity;
import dataAccess.databaseManagement.entity.ExchangeEntity;

/**
 *
 * @author Dinh
 */
public class AddNewOrderJPanel extends javax.swing.JPanel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JDialog parent;
    private Order newOrder;
    private boolean ok = false;

    public boolean isOk() {
        return ok;
    }

    public Order getNewOrder() {
        return newOrder;
    }

    /** Creates new form AddNewOrderJPanel */
    public AddNewOrderJPanel(JDialog jDialog) {
        this.parent = jDialog;
        initComponents();
        initOtherComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buySellJLabel = new javax.swing.JLabel();
        buySellJComboBox = new javax.swing.JComboBox();
        priceJLabel = new javax.swing.JLabel();
        priceJSpinner = new javax.swing.JSpinner();
        vndJLabel = new javax.swing.JLabel();
        volumeJLabel = new javax.swing.JLabel();
        volumeJSpinner = new javax.swing.JSpinner();
        cancelJButton = new javax.swing.JButton();
        okJButton = new javax.swing.JButton();
        exchangeJLabel = new javax.swing.JLabel();
        exchangeJComboBox = new javax.swing.JComboBox();
        assetJLabel = new javax.swing.JLabel();
        assetJComboBox = new javax.swing.JComboBox();

        buySellJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        buySellJLabel.setText("Buy/Sell:");

        buySellJComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Buy", "Sell" }));

        priceJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        priceJLabel.setText("Price:");

        priceJSpinner.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.0d), null, null, Double.valueOf(0.5d)));

        vndJLabel.setText("x1000 VND");

        volumeJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        volumeJLabel.setText("Volume:");

        volumeJSpinner.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.0d), null, null, Double.valueOf(10.0d)));

        cancelJButton.setText("Cancel");
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        okJButton.setText("OK");
        okJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okJButtonActionPerformed(evt);
            }
        });

        exchangeJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        exchangeJLabel.setText("Exchange:");

        exchangeJComboBox.setEditable(true);
        exchangeJComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exchangeJComboBoxActionPerformed(evt);
            }
        });

        assetJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        assetJLabel.setText("Asset:");

        assetJComboBox.setEditable(true);
        assetJComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                assetJComboBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buySellJLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buySellJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(exchangeJLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(exchangeJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(assetJLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(assetJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(priceJLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(priceJSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vndJLabel)
                .addGap(18, 18, 18)
                .addComponent(volumeJLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(volumeJSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(613, Short.MAX_VALUE)
                .addComponent(okJButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelJButton)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelJButton, okJButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buySellJLabel)
                    .addComponent(buySellJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(exchangeJLabel)
                    .addComponent(exchangeJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(assetJLabel)
                    .addComponent(assetJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(priceJLabel)
                    .addComponent(priceJSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(vndJLabel)
                    .addComponent(volumeJLabel)
                    .addComponent(volumeJSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelJButton)
                    .addComponent(okJButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void okJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okJButtonActionPerformed
        //Add new order
        ok = true;

        newOrder = new Order((AssetEntity)assetJComboBox.getSelectedItem(), (buySellJComboBox.getSelectedIndex() == 0), (Double) priceJSpinner.getValue(), (Double) volumeJSpinner.getValue());

        this.parent.dispose();
    }//GEN-LAST:event_okJButtonActionPerformed

    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        ok = false;
        this.parent.dispose();
    }//GEN-LAST:event_cancelJButtonActionPerformed

    private void exchangeJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exchangeJComboBoxActionPerformed
        if (exchangeJComboBox.getSelectedIndex() != -1) {
            assetJComboBox.setModel(new javax.swing.DefaultComboBoxModel(mainJFrame.mappingExchangeID_Assets.get((ExchangeEntity) exchangeJComboBox.getSelectedItem())));
            assetComboKeyHandler.updateListObjects();
            assetJComboBox.setSelectedIndex(0);
        }
}//GEN-LAST:event_exchangeJComboBoxActionPerformed

    private void assetJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_assetJComboBoxActionPerformed
        if (assetJComboBox.getSelectedIndex() != -1) {
        }
}//GEN-LAST:event_assetJComboBoxActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox assetJComboBox;
    private javax.swing.JLabel assetJLabel;
    private javax.swing.JComboBox buySellJComboBox;
    private javax.swing.JLabel buySellJLabel;
    private javax.swing.JButton cancelJButton;
    private javax.swing.JComboBox exchangeJComboBox;
    private javax.swing.JLabel exchangeJLabel;
    private javax.swing.JButton okJButton;
    private javax.swing.JLabel priceJLabel;
    private javax.swing.JSpinner priceJSpinner;
    private javax.swing.JLabel vndJLabel;
    private javax.swing.JLabel volumeJLabel;
    private javax.swing.JSpinner volumeJSpinner;
    // End of variables declaration//GEN-END:variables
    private ComboKeyHandler exchangeComboKeyHandler;
    private ComboKeyHandler assetComboKeyHandler;

    private void initOtherComponents() {

//        if (DataVisualizationJPanel.mappingExchangeID_Assets == null) {
//            DataVisualizationJPanel.mappingExchangeID_Assets = new TreeMap<ExchangeEntity, Object[]>();
//            ArrayList<ExchangeEntity> listAllExchangeEntitys = (new ExchangeManager()).getAllExchanges();
//            for (ExchangeEntity exchangeEntity : listAllExchangeEntitys) {
//                ArrayList<AssetEntity> listAssets = (new AssetManager()).getAssetsByExchange(exchangeEntity.getExchangeID());
//                DataVisualizationJPanel.mappingExchangeID_Assets.put(exchangeEntity, listAssets.toArray());
//            }
//        }

        exchangeJComboBox.setModel(new DefaultComboBoxModel(mainJFrame.mappingExchangeID_Assets.keySet().toArray()));
        exchangeComboKeyHandler = new ComboKeyHandler(exchangeJComboBox);
        JTextField fieldExchange = (JTextField) exchangeJComboBox.getEditor().getEditorComponent();
        fieldExchange.addKeyListener(exchangeComboKeyHandler);

        assetJComboBox.setModel(new DefaultComboBoxModel(mainJFrame.mappingExchangeID_Assets.get((ExchangeEntity) exchangeJComboBox.getSelectedItem())));
        assetComboKeyHandler = new ComboKeyHandler(assetJComboBox);
        JTextField fieldSymbol = (JTextField) assetJComboBox.getEditor().getEditorComponent();
        fieldSymbol.addKeyListener(assetComboKeyHandler);
    }

    public JDialog getParentDialog() {
        return this.parent;
    }
}
